//
//  YCNewFeatureVC.swift
//  weiBoSwift
//
//  Created by 刘胤辰 on 16/7/24.
//  Copyright © 2016年 it.com. All rights reserved.
//

import UIKit

private let cellID = "YCNewCell"

class YCNewFeatureVC: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
    
    private lazy var collectionView:UICollectionView = {
        
        let flowLayout=UICollectionViewFlowLayout()
        let col=UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        
        flowLayout.itemSize=UIScreen.mainScreen().bounds.size
        flowLayout.minimumInteritemSpacing=0
        flowLayout.minimumLineSpacing=0
        flowLayout.scrollDirection = .Horizontal
        
        col.registerClass(YCCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        col.showsHorizontalScrollIndicator = false
        col.bounces=false
        col.pagingEnabled=true
        
        col.dataSource=self
        col.delegate=self
        
        return col
    }()
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as? YCCollectionViewCell
        
        cell?.imageName="new_feature_\(indexPath.item + 1)"
        
        return cell!
    }
    
    func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.item == 3 {
            
            let yccell=cell as? YCCollectionViewCell
            
            yccell?.startBtn.hidden = false
            
            yccell?.startAnim()
        }
        
        
    }
    
    override func loadView() {
        
        view=collectionView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

class YCCollectionViewCell: UICollectionViewCell{
    
    var imageName:String?{
        
        didSet{
            
            imageView.image=UIImage(named: imageName!)
        }
    }
    
    private lazy var imageView:UIImageView = UIImageView()
    lazy var startBtn : UIButton = {
        let btn=UIButton()
        
        btn.transform=CGAffineTransformMakeScale(0, 0)

        btn.hidden=true
        btn.userInteractionEnabled=false
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setTitle("欢迎光临", forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickStart", forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        
        return btn
    }()
    
    @objc private func clickStart(){
        
        //更改根控制器(跳到欢迎界面)
        NSNotificationCenter.defaultCenter().postNotificationName("changeVC", object: "NewFeature")
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //添加
        contentView.addSubview(imageView)
        contentView.addSubview(startBtn)
        //设置约束
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView.snp_bottom).offset(-50)
        }
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    
    //开始动画
    func startAnim(){
        
        startBtn.userInteractionEnabled=true
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.startBtn.transform=CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


